package com.leen.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leen.common.vo.ResponseVO;
import com.leen.common.vo.ResultEnum;
import com.leen.common.vo.ResultVO;
import com.leen.news.dto.YxSystemUserTaskDTO;
import com.leen.news.entity.YxSystemUserTask;
import com.leen.news.mapper.YxSystemUserTaskMapper;
import com.leen.news.service.YxSystemUserTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class YxSystemUserTaskServiceImpl implements YxSystemUserTaskService {
    @Autowired
    private YxSystemUserTaskMapper yxSystemUserTaskMapper;

    @Override
    public ResultVO<String> insert(YxSystemUserTask entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        int insert = this.yxSystemUserTaskMapper.insert(entity);
        return insert > 0 ? ResultVO.success(ResultEnum.DB_SUCCESS.getMessage()) : ResultVO.failed(ResultEnum.DB_FAILED);
    }

    @Override
    public ResultVO<String> updateById(YxSystemUserTask entity) {
        entity.setUpdateTime(new Date());
        int update = this.yxSystemUserTaskMapper.updateById(entity);
        return update > 0 ? ResultVO.success(ResultEnum.DB_SUCCESS.getMessage()) : ResultVO.failed(ResultEnum.DB_FAILED);
    }

    @Override
    public ResultVO<YxSystemUserTask> selectById(Serializable id) {
        YxSystemUserTask yxSystemUserTask = this.yxSystemUserTaskMapper.selectById(id);
        return ResultVO.success(yxSystemUserTask);
    }

    @Override
    public List<YxSystemUserTask> findAll() {
        QueryWrapper<YxSystemUserTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", "0");
        return this.yxSystemUserTaskMapper.selectList(queryWrapper);
    }

    @Override
    public ResponseVO search(YxSystemUserTaskDTO yxSystemUserTaskDTO) {
        IPage<YxSystemUserTask> page = new Page<>(yxSystemUserTaskDTO.getPageNumber(), yxSystemUserTaskDTO.getPageSize());
        QueryWrapper<YxSystemUserTask> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(yxSystemUserTaskDTO.getName())) {
            queryWrapper.like("name", yxSystemUserTaskDTO.getName());
        }
        if (StringUtils.isNotBlank(yxSystemUserTaskDTO.getTaskType())) {
            queryWrapper.like("task_type", yxSystemUserTaskDTO.getTaskType());
        }
        if (yxSystemUserTaskDTO.getNumber() !=null) {
            queryWrapper.ge("number", yxSystemUserTaskDTO.getNumber());
        }
        queryWrapper.eq("is_del", "0");
        IPage<YxSystemUserTask> page1 = this.yxSystemUserTaskMapper.selectPage(page, queryWrapper);
        return new ResponseVO(page1.getTotal(), page1.getRecords());
    }
}
